package org.nmgyjt.springboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.nmgyjt.springboot.entity.expert.ExpertInfo;
import org.nmgyjt.springboot.entity.protectTarget.ProtectTargetInfo;

import java.util.List;
import java.util.Map;

@Mapper
public interface ProtectTargetMapper extends BaseMapper<ProtectTargetInfo> {
    @Select("SELECT a.* FROM (SELECT\n" +
            "\ta.org_name,\n" +
//            "\text.source_type,\n" +
            "\ta.`name`,\n" +
            "\ta.phone,\n" +
            "\tr.address,\n" +
            "\tr.dep_id,\n" +
            "\tr.protect_type source_type,\n" +
            "\tr.state,\n" +
            "\ta.org_id\n" +
            "FROM\n" +
            "\tt_cs_system_admin a\n" +
            "\tRIGHT JOIN protect_target_info r ON a.org_id = r.dep_id\n" +
            "WHERE\n" +
            "\ta.menu_id='1000000012'  ${sql}    ) a \n" +
            "\tWHERE a.org_id is not null ${sql2}")
    List<Map<String, Object>> statisticProtectTarget(String sql, String sql2);

    @Select("SELECT count(1) FROM (SELECT\n" +
            "\ta.org_name\n" +
            "FROM\n" +
            "\tt_cs_system_admin a\n" +
            "\tLEFT JOIN protect_target_info r ON a.org_id = r.dep_id\n" +
            "WHERE\n" +
            "\ta.menu_id='1000000012' AND r.state <> '1' \n" +
            "\tAND a.superior_org=#{createdBy, jdbcType=VARCHAR}) a")
    Integer statisticProtectTargetCount(@Param("createdBy") String createdBy);

    @Select("SELECT count(1) FROM (SELECT\n" +
            "\tr.protect_type_code\n" +
            "FROM\n" +
            "\tt_cs_system_admin a\n" +
            "\tLEFT JOIN protect_target_info r ON a.org_id = r.dep_id\n" +
            "WHERE\n" +
            "\ta.menu_id='1000000012' AND r.state <> '1' \n" +
            "\tAND a.superior_org=#{createdBy, jdbcType=VARCHAR}\n" +
            "\tgroup by r.protect_type_code) a")
    Integer statisticProtectTargetTypeCount(@Param("createdBy") String createdBy);

    @Select("SELECT a.second_type_code,a.second_type,a.allNum,IFNULL(b.done,0) done,allNum-IFNULL(done,0) undone FROM (SELECT\n" +
            "r.second_type_code,r.second_type,COUNT(1) allNum\n" +
            "FROM\n" +
            "t_cs_system_admin a\n" +
//            "RIGHT JOIN t_cs_system_source_ext ext ON a.org_id = ext.org_id\n" +
            "LEFT JOIN protect_target_info r ON a.org_id = r.dep_id\n" +
            "WHERE\n" +
            "a.menu_id='1000000012' AND r.state <> '1' \n" +
            "AND a.superior_org=#{createdBy, jdbcType=VARCHAR}\n" +
//            "AND ext.org_id IS NOT NULL\n" +
            "AND r.protect_type_code=#{projectTypeCode, jdbcType=VARCHAR}\n" +
            "group by r.second_type_code,r.second_type) a LEFT JOIN \n" +
            "(SELECT\n" +
            "r.second_type_code,r.second_type,COUNT(1) done\n" +
            "FROM\n" +
            "t_cs_system_admin a\n" +
//            "RIGHT JOIN t_cs_system_source_ext ext ON a.org_id = ext.org_id\n" +
            "LEFT JOIN protect_target_info r ON a.org_id = r.dep_id\n" +
            "WHERE\n" +
            "a.menu_id='1000000012' AND r.state = '0' \n" +
            "AND a.superior_org=#{createdBy, jdbcType=VARCHAR}\n" +
//            "AND ext.org_id IS NOT NULL\n" +
            "AND r.protect_type_code=#{projectTypeCode, jdbcType=VARCHAR}\n" +
            "group by r.second_type_code,r.second_type) b ON a.second_type_code=b.second_type_code " +
            "ORDER BY a.second_type_code asc")
    Map<String, Object> statisticProtectTargetTypeCountStatus(@Param("createdBy") String createdBy, @Param("projectTypeCode") String projectTypeCode);

    @Update("UPDATE protect_target_info a SET a.state '1' WHERE a.dep_id = #{depId,jdbcType=VARCHAR}")
    Integer deleteByDepId(@Param("depId") String depId);
}
